package com.hyyt.shopjsp.home.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.pojo.ShopHomeMiddleCategory;
import com.hyyt.shopjsp.home.service.IShopHomeMiddleCategoryService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.tree.NodeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/28 0024上午 10:27.
 */
@Controller
@RequestMapping("/back/shopHomeMiddleCategory")
public class ShopHomeMiddleCategoryController extends BaseController {
	private Logger log = Logger.getLogger(ShopHomeMiddleCategoryController.class);
	private String basePath = "/back/shop/homeMiddleCategory/";

	@Resource
	private IShopHomeMiddleCategoryService shopHomeMiddleCategoryService;//首页中间分类service
	/**
	 * 进入首页中间分类信息管理
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoShopHomeMiddleCategoryTree")
	public String gotoShopHomeMiddleCategoryTree(){
		return basePath+"shopHomeMiddleCategoryList";
	}

	/**
	 * 根据id展示左侧首页楼层树形结构
	 * @param response
	 */
	@RequestMapping("getNodes")
	public void getNodes(HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		String [] selectColmunsArray={"categoryId","categoryName","parentId","isLeaf"};
		param.put("selectColmunsArray",selectColmunsArray);
		param.put("sortColumns", "sortCode asc");
		List<ShopHomeMiddleCategory> categoryList=shopHomeMiddleCategoryService.queryAllShopHomeMiddleCategory(param);
		List nodeList = new ArrayList();
		HashMap nodeMap = new HashMap();
		for(int i = 0; i < categoryList.size(); i++) {
			ShopHomeMiddleCategory shmc = (ShopHomeMiddleCategory)categoryList.get(i);
			nodeMap = new HashMap();
			nodeMap.put("id",shmc.getCategoryId());
			nodeMap.put("text", shmc.getCategoryName());
			nodeMap.put("isLeaf", shmc.getIsLeaf());
			String parentId = String.valueOf(shmc.getParentId());
			if (Utils.objectIsEmpty(parentId)) {
				parentId = "0";
			}
			nodeMap.put("parentId",parentId);
			nodeList.add(nodeMap);
		}
		//获取分类的json字符串信息
		String jsonInfo= NodeUtil.getNodeJson(nodeList,"首页楼层分类");
		ResultMsg.create(jsonInfo, response, false);
	}

	/**
	 * 查询指定分类下的子分类列表
	 *
	 * @param page
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "listSubCategoryByParentId", method = RequestMethod.POST)
	@ResponseBody
	public void listSubCategoryByParentId(Integer parentId, String categoryName, Page page, HttpServletResponse response) {
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(parentId)) {
			param.put("parentId", String.valueOf(parentId));
		}else{//默认为所有一级节点
			param.put("parentId","0");
		}
		if (Utils.objectIsNotEmpty(categoryName)) {
			param.put("categoryName", categoryName);
		}
		int totalRecordCount = shopHomeMiddleCategoryService.countShopHomeMiddleCategory(param);
		//定义查询的字段
		String [] selectColmunsArray={"categoryId","categoryName","parentId","sortCode"};
		param.put("selectColmunsArray",selectColmunsArray);
		param.put("sortColumns", "sortCode asc");
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopHomeMiddleCategory> list = shopHomeMiddleCategoryService.queryShopHomeMiddleCategory(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		param.put("parentId", parentId);
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 根据首页中间分类id获得分类对象
	 * @param categoryId
	 * @return
	 */
	@RequestMapping(value="getShopHomeMiddleCategory",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getShopHomeMiddleCategory(Integer categoryId){
		if(Utils.objectIsNotEmpty(categoryId)){
			ShopHomeMiddleCategory shopHomeMiddleCategory = shopHomeMiddleCategoryService.loadShopHomeMiddleCategory(categoryId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopHomeMiddleCategory));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 保存或修改首页中间分类
	 * @param shopHomeMiddleCategory
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateShopHomeMiddleCategory",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateShopHomeMiddleCategory(ShopHomeMiddleCategory shopHomeMiddleCategory){
		return shopHomeMiddleCategoryService.saveOrUpdateShopHomeMiddleCategory(shopHomeMiddleCategory);
	}
	/**
	 * 删除首页中间分类
	 * @param categoryId
	 * @return
	 */
	@RequestMapping(value="deleteShopHomeMiddleCategory",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteArea(Integer categoryId){
		return shopHomeMiddleCategoryService.deleteShopHomeMiddleCategory(categoryId);
	}

	/**
	 * 通过树来维护列表
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoShopHomeMiddleCategoryTablePage")
	public String gotoShopHomeMiddleCategoryTablePage( Map<String, Object> param){
		param.put("categoryId","1");
		return basePath+"shopHomeMiddleCategoryTree_listLink";
	}
}
